<?php
class Application_Model_CalCorelation
{
	protected $_dbTable;
	public $_mStockCode;
	public $_mStockExchange;
	public $_mData;

	public function getStockCode(){
		return $this->_mStockCode;
	}
	public function setStockCode($value){
		$this->_mStockCode = $value;
		return $this;
	}
	public function getStockExchange(){
		return $this->_mStockExchange;
	}
	public function setStockExchange($value){
		$this->_mStockExchange = $value;
		return $this;
	}
	public function getData(){
		return $this->_mData;
	}
	public function setData($value){
		$this->_mData = $value;
		return $this;
	}


	public function setDbTable($dbTable){
		if (is_string($dbTable))
			$dbTable = new $dbTable();
		if (!$dbTable instanceof Zend_Db_Table_Abstract)
			throw new Exception('Invalid table data gateway provided');
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable(){
		if (null === $this->_dbTable)
			$this->setDbTable('Application_Model_DbTable_CalCorelation');
		return $this->_dbTable;
	}
	public function save(Application_Model_CalCorelation $old = null){
		$item = $this;
		$data = array();
		if(isset($item->_mStockCode)) $data['stock_code'] = $item->_mStockCode;
		if(isset($item->_mStockExchange)) $data['stock_exchange'] = $item->_mStockExchange;
		if(isset($item->_mData)) $data['data'] = $item->_mData;

		if($old === null)
			$this->getDbTable()->insert($data);
		else{
			$key = array();
			$old->_mStockCode === null ? $key[] = 'stock_code is NULL' : $key['stock_code = ?'] =  $old->_mStockCode;
			$old->_mStockExchange === null ? $key[] = 'stock_exchange is NULL' : $key['stock_exchange = ?'] =  $old->_mStockExchange;

			$this->getDbTable()->update($data, $key);
		}
	}
	public function delete(){
		$item = $this;
		$old = $item;
		$key = array();
			$old->_mStockCode === null ? $key[] = 'stock_code is NULL' : $key['stock_code = ?'] =  $old->_mStockCode;
			$old->_mStockExchange === null ? $key[] = 'stock_exchange is NULL' : $key['stock_exchange = ?'] =  $old->_mStockExchange;

		$this->getDbTable()->delete($key);
	}
	public function fetchAll($where = null, $order = null, $count = null, $offset = null){
		$resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
		$entries   = array();
		foreach ($resultSet as $row) {
			$item = new Application_Model_CalCorelation();
			if(isset($row->stock_code))$item->_mStockCode = $row->stock_code;
			if(isset($row->stock_exchange))$item->_mStockExchange = $row->stock_exchange;
			if(isset($row->data))$item->_mData = $row->data;

			$entries[] = $item;
		}
		return $entries;
	}
}
